//
//  ViewController.swift
//  Tableview
//
//  Created by lujun on 16/4/11.
//  Copyright © 2016年 lujun. All rights reserved.
//

import UIKit
import SwiftyJSON

//pod install --verbose --no-repo-update
//http://www.cocoachina.com/bbs/read.php?tid-280971.html 继承 UITableViewController
//http://www.tuicool.com/articles/ZzQJbaY  自动索引


class ViewController: UIViewController {
    
    let locationPageTableViewController = LocationPageTableViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.addSubview(locationPageTableViewController.tableView)
        
    }
}

//UITableViewCellSeparatorStyleSingleLine

// 选择区域的 tableview
class LocationPageTableViewController: UITableViewController{
    
    // 依赖于后端把数据按照字母分组
    // 理论上 ios 也可以进行分组处理，参考 http://www.tuicool.com/articles/ZzQJbaY
    
    let items = "[{\"title\":\"A\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]},{\"title\":\"B\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]},{\"title\":\"C\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]},{\"title\":\"D\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]},{\"title\":\"E\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]},{\"title\":\"F\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]},{\"title\":\"G\",\"areas\":[{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"},{\"cityName\":\"青岛\",\"title\":\"青岛卫计委\",\"keyWord\":\"青岛\",\"unionId\":29,\"describe\":\"青岛已开通3家医院\"}]}]"
    
    //当前定位城市标题
    let locationTitle = "当前定位城市："
    //当前所定位到的城市名称
    let location = "杭州"
    
    //不支持地区提示
    let notSupportedTip = "(未开通服务，请选择已开通的城市)"
    let fontName = "Arial";
    
    var json:JSON?
    
    init() {
        
        super.init(style: .Grouped)
        
        // not initalized at super.init
        let data = items.dataUsingEncoding(NSUTF8StringEncoding);
        json = JSON(data: data!)
        
        //  json 数据
        print(json)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // 某个 section 中有几条数据（row count）
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        if( section == 0 ){
            return 1
        }
        return json![section-1]["areas"].count
        
    }
    
    // section 个数
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        //因为会在头部多插入一个当前地址
        return json!.count+1;
    }
    
    // section footer title
    override func tableView(tableView: UITableView, titleForFooterInSection section: Int) -> String? {
        
        return nil
    }
    
    // section header title
    override func tableView(tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
        
        
        if( section == 0 ){
            return nil
        }
        
        return json![section-1]["title"].string;
    }
    
    // render cell
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = UITableViewCell(style: UITableViewCellStyle.Value1, reuseIdentifier:"i"+String(indexPath.section) + String(indexPath.row))
        
        
        if(indexPath.section >= 1){
            
            cell.textLabel?.text = json![indexPath.section-1]["areas"][indexPath.row]["cityName"].string
            
            cell.detailTextLabel?.text = json![indexPath.section-1]["areas"][indexPath.row]["describe"].string
            cell.detailTextLabel?.font = UIFont(name:fontName, size:14);
            
        }else{
            //渲染当前位置
            cell.textLabel?.text = locationTitle + location;//"当前定位城市：杭州";
            cell.textLabel?.font = UIFont(name:fontName, size: 14)
            cell.detailTextLabel?.text = nil;
            let detailTextLabel = UILabel(frame: CGRectMake(150, 18, 200, 12))
            detailTextLabel.text = notSupportedTip
            detailTextLabel.font = UIFont(name:fontName, size:12);
            detailTextLabel.textColor = UIColor.grayColor()
            cell.addSubview(detailTextLabel)
        }
        
        return cell
        
    }
    
    //索引
    override func sectionIndexTitlesForTableView(tableView: UITableView) -> [String]? {
        
        var titles = [String]()
        
        titles.append("#")
        for(var i=0; i<json?.count; i++){
            titles.append(json![i]["title"].string!)
        }
        
        
        return titles;
    }
    
    
    
}